package cn.bone.motan.jingzhouArea.gateway.server;

import cn.bone.motan.common.MoRequest;
import cn.bone.motan.common.MoResponse;
import cn.bone.motan.demo1.api.RemoteApi;
import cn.bone.motan.demo1.api.ServerApi;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by MD-01 on 2017/3/16.
 * 处理其他gateway过来的请求
 */
@Service
public class RemoteService implements RemoteApi {

    @Resource
    private ServerApi serverApi;
    @Resource GatewayService gatewayService;
    private Logger logger = LoggerFactory.getLogger("gateway");

    @Override
    public MoResponse execute(MoRequest request) {

        String toSign = request.getTo();
        logger.info("来自其他域的请求.目的地是:"+toSign);
        if(StringUtils.isEmpty(toSign)){
            return MoResponse.error("错误的sign");
        }
        //访问本域,把请求转发到server
        if(toSign.equals("jzq.jz.hb.cn")){
            logger.info("访问本域");
            return serverApi.execute(request);
        }
        //消息不是来自本域,通过gateway转发到其他gateway
        else{
            logger.info("不属于本域,转发到其他gateway");
            return gatewayService.execute(request);
        }
    }
}
